package controllers;

import play.mvc.*;
import play.data.validation.*;

import models.*;

public class Application extends Controller {
    
    @Before
    static void addUser() {
        User user = connected();
        if(user != null) {
            renderArgs.put("user", user);
        }
    }
    
    static User connected() {
        if(renderArgs.get("user") != null) {
            return renderArgs.get("user", User.class);
        }
        String usermail = session.get("user");
        if(usermail != null) {
            return User.find("byUsermail", usermail).first();
        } 
        return null;
    }


    public static void index() {
        if(connected() != null) {
            Komponents.index();
        }
        render();
    }
    
    public static void new_user() {
        render();
    }
    
    public static void admin_login() {
        render("@admin_login");
    }
    
    public static void new_project() {
        render();
    }
    
    public static void saveUser(@Valid User user) {
        if(validation.hasErrors()) {
            render("@new_user", user);
        }
        user.create();
        session.put("user", user.usermail);
        flash.success("Welcome, " + user.name);
        Komponents.index(); 
    }
    
    public static void saveProject(@Valid Project project) {
        if(validation.hasErrors()) {
            render("@new_project", project);
        }
        project.create();
        session.put("project", project.project_number);
        flash.success("Project " + project.project_number + " has now been added");
        index(); 
    }   
    
    public static void LinkUser(@Valid Project project_number,@Valid User usermail) {
    	
    	
        //if(user != null || project != null) {
        //    //session.put("user", user.username);
        //    flash.success("Usermail Valid! " + user + " Project Nr. Valid! " + project);
        //    index(); 
        //}
        
        if(params.get("register") != null) {
            //booking.create();
        	//user_projects.create();
        	User user = User.find("byUsermail", usermail).first();
        	Project project = Project.find("byProject_number", project_number).first();
        	flash.success("params! " + project.project_number + " ... " + user.usermail);
            index();
        }
        render("@user_projects");
        //project.create();
        //session.put("user", project.project_number);
        //flash.success("Project " + project.project_number + " has now been added");
        
    }  
    
    
    public static void login(String usermail) {
        User user = User.find("byUsermail", usermail).first();
        
        if(user != null) {
            session.put("user", user.usermail);
            flash.success("Welcome, " + user.name);
            Komponents.index(); 
        }
        flash.put("usermail", usermail);
        flash.error("Login failed " + user);
        index();
    }
    
    public static void Adminlogin(String adminmail, String password) {
        Admin admin = Admin.find("byAdminmailAndPassword", adminmail, password).first();
        
        if(admin != null) {
            session.put("admin", admin.adminmail);
            flash.success("Welcome, " + admin.name);
            render("@admin_login"); 
        }
        flash.put("adminmail", adminmail);
        flash.error("Login failed " + admin);
        render("@admin_login");
    }
    
    public static void logout() {
        session.clear();
        index();
    }
    


}